//定义目录
const fs = require('fs');
const path = require('path');
const express = require('express');
const app = express();

const { createProxyMiddleware } = require('http-proxy-middleware')

//vue目录
app.use(express.static(path.resolve(__dirname, './dist')))

// 参考vueconfig文件
const proxyOptions = {
    target: 'http://localhost:3000', // target host
    changeOrigin: true, // needed for virtual hosted sites
    ws: true, // proxy websockets
    pathRewrite: {
        '^/api': '/api' // rewrite path
    },
}

app.use('/api', createProxyMiddleware(proxyOptions))

app.get('*', function (req, res) {
    const html = fs.readFileSync(path.resolve(__dirname, './dist/index.html'), 'utf-8')
    res.send(html)
})
app.listen(80, () => {
    console.log('http://localhost');
});